﻿using System;
using System.Reflection;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

using YhmOA.Common;
using YhmOA.IDAL;

namespace YhmOA
{
    public class FormPage<T> : BasePage where T : class
    {
        #region Fields and Properties
        /// <summary>
        /// 数据访问接口
        /// </summary>
        private ITDal<T> _dal;
        /// <summary>
        /// 数据访问接口
        /// </summary>
        public ITDal<T> Dal
        {
            get { return _dal ?? DataProvider.GetGEFDal<ITDal<T>>(); }
        }
        /// <summary>
        /// 默认实体
        /// </summary>
        private T _local;
        /// <summary>
        /// 默认实体
        /// </summary>
        public T Local
        {
            get { return _local; }
            set { _local = value; }
        }

        /// <summary>
        /// 页面模式
        /// </summary>
        public YhmOA.Model.FormMode Mode
        {
            get
            {
                return (YhmOA.Model.FormMode)Convert.ToInt32(Request.QueryString["mode"] ?? "0");
            }
        }
        /// <summary>
        ///<para>内容页在数据库中对应的</para>
        /// <para>如果是添加时ID默认值为0</para>
        /// </summary>
        public int _ID
        {
            get
            {
                try
                {
                    return Convert.ToInt32(Request.QueryString["ID"] ?? "0");
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion

         protected void Page_PreInit(object sender, EventArgs e)
        {
            string themeName = "Office2003 Blue";
            this.Theme = themeName;
        }

        #region 根据ID获取对象并且加载到页面上
        public void LoadData(bool isReadOnly)
        {
            if (this._ID > 0 && !this.IsAjaxPostBack)
            {
                _local = DataProvider.GetGEFDal<ITDal<T>>().GetModelByID(this._ID);
                foreach (PropertyInfo item in typeof(T).GetProperties())
                {
                    if (item.Name != "ID")
                    {
                        WebControl wc = this.Page.FindControl("txt_" + item.Name) as WebControl;
                        if (wc != null)
                        {
                            string typeName = wc.GetType().Name;
                            switch (typeName)
                            {
                                case "TextBox":
                                    (wc as TextBox).Text = this.GetTypeToType(item.GetValue(_local, null));
                                    break;
                                case "SelectBox":
                                    (wc as YhmOA.SelectBox.SelectBox).Text = this.GetTypeToType(item.GetValue(_local, null));
                                    break;
                                case "DropDownList":
                                    (wc as DropDownList).SelectedValue = this.GetTypeToType(item.GetValue(_local, null));
                                    break;
                                #region DevExpress
                                case "ASPxTextBox":
                                    (wc as ASPxTextBox).Text = this.GetTypeToType(item.GetValue(_local, null));
                                    (wc as ASPxTextBox).ReadOnly = isReadOnly;
                                    break;
                                case "ASPxDateEdit":
                                    if ((wc as ASPxDateEdit).EditFormat == EditFormat.DateTime)
                                        (wc as ASPxDateEdit).Text = Convert.ToDateTime(item.GetValue(_local, null)).ToString("yyyy-MM-dd HH:mm");
                                    else
                                        (wc as ASPxDateEdit).Text = Convert.ToDateTime(item.GetValue(_local, null)).ToShortDateString();
                                    (wc as ASPxDateEdit).ReadOnly = isReadOnly;
                                    break;
                                case "ASPxComboBox":
                                    (wc as ASPxComboBox).Text = this.GetTypeToType(item.GetValue(_local, null));
                                    (wc as ASPxComboBox).ReadOnly = isReadOnly;
                                    break;
                                case "ASPxMemo":
                                    (wc as ASPxMemo).Text = this.GetTypeToType(item.GetValue(_local, null));
                                    (wc as ASPxMemo).ReadOnly = isReadOnly;
                                    break;
                                case "ASPxImage":
                                    (wc as ASPxImage).ImageUrl = this.GetTypeToType(item.GetValue(_local, null));
                                    (wc as ASPxImage).ReadOnly = isReadOnly;
                                    break;
                                #endregion
                            }
                        }
                    }
                }
            }
        }

        public void LoadData()
        {
            LoadData(false);
        }
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            TextBox txt_GUID = Page.FindControl("txt_GUID") as TextBox;
            if (txt_GUID != null)
            {
                if (this._ID == 0)
                {
                    txt_GUID.Text = Guid.NewGuid().ToString();
                }
            }
        }

        #region 根据类型创建类型实例
        /// <summary>
        /// 根据类型创建类型实例
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns>类型实例</returns>
        public T CreateInstance()
        {
            T local = Activator.CreateInstance<T>();
            foreach (PropertyInfo item in typeof(T).GetProperties())
            {
                if (item.Name == "ID")
                {
                    item.SetValue(local, this._ID, null);
                }
                else
                {
                    WebControl wc = this.Page.FindControl("txt_" + item.Name) as WebControl;
                    if (wc != null)
                    {
                        string typeName = wc.GetType().Name;
                        string objValue = null;
                        switch (typeName)
                        {
                            case "TextBox":
                                objValue = (wc as TextBox).Text;
                                break;
                            case "SelectBox":
                                objValue = (wc as YhmOA.SelectBox.SelectBox).Text;
                                break;
                            case "DropDownList":
                                objValue = (wc as DropDownList).SelectedValue;
                                break;
                            #region DevExpress
                            case "ASPxTextBox":
                                objValue = (wc as ASPxTextBox).Text;
                                break;
                            case "ASPxDateEdit":
                                objValue = (wc as ASPxDateEdit).Text;
                                break;
                            case "ASPxComboBox":
                                objValue = (wc as ASPxComboBox).Text;
                                break;
                            case "ASPxMemo":
                                objValue = (wc as ASPxMemo).Text;
                                break;
                            case "ASPxImage":
                                objValue = (wc as ASPxImage).ImageUrl;
                                break;
                            #endregion
                        }
                        item.SetValue(local, this.GetTypeToType(item.PropertyType.Name, objValue), null);
                    }
                }
            }
            return local;
        }
        #endregion

        #region 根据类型名称将对象转换成相应类型
        /// <summary>
        /// 根据类型名称将对象转换成相应类型
        /// </summary>
        /// <param name="typeName">类型名称</param>
        /// <param name="obj">对象</param>
        /// <returns>指定类型的对象</returns>
        private object GetTypeToType(string typeName, object obj)
        {
            
            switch (typeName)
            {
                case "Int32":
                    obj = obj.ToString() == "" ? "0" : obj;
                    obj = Convert.ToInt32(obj);
                    break;
                case "DateTime":
                    obj = obj.ToString() == "" ? "1900-01-01" : obj;
                    obj = Convert.ToDateTime(obj);
                    break;
                case "Decimal":
                    obj = obj.ToString() == "" ? "0" : obj;
                    obj = Convert.ToDecimal(obj);
                    break;
                default:
                    obj = obj.ToString();
                    break;
            }
            return obj;
        }
        #endregion

        #region 将对象转换成相应类型
        /// <summary>
        /// 将对象转换成相应类型
        /// </summary>
        /// <param name="obj">要转换的类型</param>
        /// <returns>返回的字符串</returns>
        private string GetTypeToType(object obj)
        {
            string result = "";
            if (obj.GetType().Name == "Decimal")
            {
                result = Convert.ToDecimal(obj).ToString("#.00##");
            }
            else if (obj.GetType().Name == "DateTime")
            {
                result = Convert.ToDateTime(obj).ToString("yyyy-MM-dd");
            }
            else
            {
                result = obj.ToString();
            }
            return result;
        }
        #endregion

        #region 权限相关
        /// <summary>
        /// 关于服务器控件的服务端事件的权限
        /// </summary>
        /// <param name="FormMode">权限的类型</param>
        /// <param name="ctrlID">服务器控件ID</param>
        [AjaxMethod]
        public void RoleVerificationCtrl(string FormMode, string ctrlID)
        {
            bool flag = true;
            //flag = false;
            if (flag)
            {
                ExecClientScript("__doPostBack('" + ctrlID + "', '');");
            }
            else
            {
                MessageBox("您没有权限");
            }
        }
        /// <summary>
        /// 关于客户端控件的权限
        /// </summary>
        /// <param name="FormMode">权限的类型</param>
        /// <param name="script">有权限时执行的代码</param>
        [AjaxMethod]
        public void RoleVerification(string FormMode, string script)
        {
            bool flag = true;
            //flag = false;
            if (flag)
            {
                if (FormMode == "3")
                {
                    SetNotAjaxRenderControls(this.Page.FindControl("gvShow"));
                }
                this.ExecClientScript(script);
            }
            else
            {
                MessageBox("您没有权限");
            }
        }
        #endregion
    }
}
